package algorithm;

public class T00274 {
    public static void main(String[] args) {
        int[] nums = {100,0};
        System.out.println(new Solution().hIndex(nums));
    }

    static class Solution {
        public int hIndex(int[] citations) {
            quickSort(citations, 0, citations.length - 1);
            int h = 0;
            for (int i = 0; i < citations.length; i ++) {
                if (citations[i] >= (i + 1)) {
                    h = Math.min(citations[i], i + 1);
                }
            }
            return h;
        }

        public void quickSort(int[] arr, int s, int e) {
            if (s >= e) {
                return;
            }
            int mid = s + (e - s) / 2;
            int temp = arr[mid];
            swap(arr, s, mid);
            int i = s, j = e;
            while (i < j) {
                while (i < j && arr[j] <= temp) j --;
                swap(arr, i, j);
                while (i < j && arr[i] >= temp) i ++;
                swap(arr, i, j);
            }
            arr[i] = temp;
            quickSort(arr, s, i - 1);
            quickSort(arr, i + 1, e);
        }

        public void swap(int[] arr, int i, int j) {
            if (i == j) {
                return;
            }
            int t = arr[i];
            arr[i] = arr[j];
            arr[j] = t;
        }
    }
}
